using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ASP_TF2LogParser.UserControls
{
    public partial class PlayerListControlByLogId : System.Web.UI.UserControl
    {
        LogParser.DA.PlayerModel _model;

        protected void Page_Load( object sender, EventArgs e )
        {
            //_model = new LogParser.DA.PlayerModel();
            
            
            //LogParser.DA.DataObjects.PlayerStatsDS.PlayerShortStatsDataTable pssdt = _model.GetPlayerShortStats();

            //if ( !Page.IsPostBack )
            //    pssdt.DefaultView.Sort = "Kills DESC, KillDeathRatio DESC, Deaths Desc";

            //this.playerListgv.DataSourceID = String.Empty;
            //this.playerListgv.DataSource = pssdt;
            //this.playerListgv.DataBind();
            string logIdList = string.Empty;

            int leagueId = Convert.ToInt32( Request[ "leagueId" ] );
            List<int> logList = LogParser.DA.LogFileModel.GetLogIdList( leagueId );

            foreach ( int logId in logList )
            {
                if ( logIdList != string.Empty )
                    logIdList += ",";

                logIdList += logId.ToString();
            }

            if ( logIdList != string.Empty )
            {
                NoLeagueIdErrorMessage.Visible = false;
                playerListFilteredContainer.Visible = true;
                BindDatatable( logIdList );
            }
            else
            {
                NoLeagueIdErrorMessage.Visible = true;
                playerListFilteredContainer.Visible = false;
            }

        }

        private void BindDatatable(string logIdList)
        {
           LogParser.DA.DataObjects.PlayerStatsDS.PlayerShortStatsDataTable dt = LogParser.DA.PlayerModel.GetPlayerShortStatsByLogId( logIdList );
           playerListgv.DataSource = dt;
           playerListgv.DataBind();
        }


        //#region Sorting

        //protected void playerListgv_Sorting( object sender, GridViewSortEventArgs e )
        //{
        //    DataTable dataTable = playerListgv.DataSource as DataTable;

        //    if ( dataTable != null )
        //    {
        //        //DataView dataView = new DataView( dataTable );
        //        //dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );


        //        dataTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );
        //        playerListgv.DataSource = dataTable;
        //        playerListgv.DataBind();
        //    }



        //}

        //public SortDirection GridViewSortDirection
        //{
        //    get
        //    {
        //        if ( ViewState[ "sortDirection" ] == null )
        //            ViewState[ "sortDirection" ] = SortDirection.Ascending;

        //        SortDirection returnDirection = ( SortDirection ) ViewState[ "sortDirection" ];

        //        if ( ( SortDirection ) ViewState[ "sortDirection" ] == SortDirection.Ascending )
        //            ViewState[ "sortDirection" ] = SortDirection.Descending;
        //        else
        //            ViewState[ "sortDirection" ] = SortDirection.Ascending;



        //        return returnDirection;
        //    }
        //    set { ViewState[ "sortDirection" ] = value; }
        //}

        //private string ConvertSortDirectionToSql( SortDirection sortDirection )
        //{
        //    string newSortDirection = String.Empty;

        //    switch ( sortDirection )
        //    {
        //        case SortDirection.Ascending:
        //            newSortDirection = "ASC";
        //            break;

        //        case SortDirection.Descending:
        //            newSortDirection = "DESC";
        //            break;
        //    }

        //    return newSortDirection;
        //}

        //#endregion


        public string GenerateUrl( string playerName, string playerId )
        {

            string returnUrl = "";


            returnUrl = "<a href='PlayerFiltered.aspx?playerId=" + playerId;

            if ( Request[ "leagueId" ] != null )
                returnUrl += "&leagueId=" + Request[ "leagueId" ];

            returnUrl += "'>" + playerName + "</a>";
            
            return returnUrl;


        }

    }
}